<template>
  <div class="app-container">
    <div class="filter-container">
      <el-input
        v-model="listQuery.name"
        placeholder="搜索内容"
        style="width: 20%;margin-right: 10px;"
        class="filter-item"
        clearable
        @keyup.enter.native="searchClick"
      />

      <el-button
        class="filter-item"
        type="primary"
        icon="el-icon-search"
        @click="searchClick"
      >搜索
      </el-button>
      <el-button
        v-permission="'role.save'"
        class="filter-item"
        style="margin-left: 10px;"
        type="primary"
        icon="el-icon-edit"
        @click="addClick"
      >新增
      </el-button>

      <el-table
        :data="tableData"
        style="width: 100%;margin-bottom: 20px;"
        row-key="id"
        border
      >
        <el-table-column prop="id" label="ID" />
        <el-table-column prop="name" label="角色名称" />
        <el-table-column prop="createTime" label="创建时间" />
        <el-table-column label="操作">
          <template slot-scope="scope">
            <el-button v-permission="'role.save'" type="text" size="small" @click="editClick(scope.row)">编辑</el-button>
            <el-button v-permission="'role.delete'" type="text" size="small" @click="deleteClick(scope.row)">删除</el-button>
          </template>
        </el-table-column>
      </el-table>
      <pagination
        v-show="page.total>0"
        :total="page.total"
        :page.sync="listQuery.current"
        :limit.sync="listQuery.size"
        @pagination="list"
      />
    </div>

    <el-dialog :title="form.id ? '编辑':'新增' " :visible.sync="editDialogTableVisible" @opened="editDialogTableOpened">
      <el-form ref="form" :model="form" label-width="80px" size="small" @keyup.enter.native="save">
        <el-form-item label="角色名称" prop="name">
          <el-input v-model="form.name" clearable placeholder="name" />
        </el-form-item>
        <el-form-item label="菜单权限">
          <el-tree
            ref="tree"
            :data="treeData"
            show-checkbox
            default-expand-all
            node-key="id"
            highlight-current
            :props="defaultProps"
          />
        </el-form-item>

      </el-form>

      <span slot="footer" class="dialog-footer">
        <el-button size="small" @click="editDialogTableVisible = false">取消</el-button>
        <el-button size="small" type="primary" @click="save">保存</el-button>
      </span>
    </el-dialog>

  </div>
</template>

<script>
import request from '@/utils/request'
import Pagination from '@/components/Pagination' // secondary package based on el-pagination

export default {
  name: 'RoleList',
  components: { Pagination },
  data() {
    return {
      editDialogTableVisible: false,
      form: {},
      treeData: [],
      defaultProps: {
        children: 'children',
        label: 'title'
      },
      listQuery: {
        name: '',
        current: 1
      },
      page: {
        total: 0
      },
      tableData: []
    }
  },
  created() {
    this.getMenus()
    this.list()
  },
  methods: {
    list: function() {
      request({
        url: '/api/role/list',
        method: 'post',
        data: this.listQuery
      }).then(res => {
        this.tableData = res.data.records
        this.page.total = res.data.total
      })
    },
    getMenus: async function() {
      await request({
        url: '/api/menu/list',
        method: 'post',
        data: this.listQuery
      }).then(res => {
        const menus = res.data
        const tmp = [{ id: 0, title: '全部', children: menus }]
        this.treeData = tmp
      })
    },
    editDialogTableOpened: function() {
      this.$refs.tree.setCheckedKeys([])
      if (this.form.id) {
        // console.log(this.form.menuIds)
        if (this.form.menuIds && this.form.menuIds != '') {
          const arr = this.form.menuIds.split(',')
          arr.forEach(v => {
            this.$refs.tree.setChecked(v, true, false)
          })
        }
      }
    },
    addClick: function() {
      this.getMenus()
      this.editDialogTableVisible = true
      this.form = {}
    },
    save: function() {
      const ids = this.$refs.tree.getHalfCheckedKeys()
      this.$refs.tree.getCheckedKeys().forEach(v => {
        ids.push(v)
      })
      // ids = this.removeNullArray(ids)
      this.form.menuIds = ids.join(',')
      request({
        url: '/api/role/save',
        method: 'post',
        data: this.form
      }).then(res => {
        this.$message({ message: '操作成功', type: 'success' })
        this.editDialogTableVisible = false
        this.list()
      })
    },
    editClick: function(row) {
      this.getMenus()
      this.editDialogTableVisible = true
      this.form = this.cloneObject(row)
    },
    deleteClick: function(row) {
      this.$confirm('确定要删除吗？').then(() => {
        request({
          url: '/api/role/delete',
          method: 'post',
          data: { id: row.id }
        }).then(res => {
          this.$message({ message: '操作成功', type: 'success' })
          this.list()
        })
      })
    },
    searchClick: function() {
      this.listQuery.current = 1
      this.list()
    }

  }
}
</script>

<style scoped>

</style>
